﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Collections.ObjectModel;
using Administratation.Models;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Input;
using Administratation.Commands;

namespace Administratation.ViewModels
{
    public class CountriesViewModel : ViewModelBase
    {
        private CountriesModel model;
        private ICommand saveCountry;
        private ICommand deleteCountry;
        private ICommand addCountry;

        private ICommand saveTown;
        private ICommand deleteTown;
        private ICommand addTown;

        public CountriesViewModel()
        {
            this.model = new CountriesModel();
            var viewCountries = this.GetDefaultCountriesView();
            viewCountries.CurrentChanged += new EventHandler(viewCountries_CurrentChanged);
        }

        public void SubscribeToChangedEvent()
        {
            var viewTowns = this.GetDefaultTownsView();
            viewTowns.CurrentChanged += new EventHandler(viewTowns_CurrentChanged);
        }

        public ObservableCollection<Country> Countries
        {
            get
            {
                return this.model.Countries;
            }
        }

        public ObservableCollection<Town> Towns
        {
            get
            {
                if (this.SelectedCountry != null)
                {
                    int countryID = this.SelectedCountry.CountryID;
                    if (countryID != null)
                    {
                        this.model.SetTownsByCountry(countryID);
                    }
                }
                return this.model.Towns;
            }
        }

        private ICollectionView GetDefaultCountriesView()
        {
            var view = CollectionViewSource.GetDefaultView(this.Countries);
            return view;
        }

        private ICollectionView GetDefaultTownsView()
        {
            var view = CollectionViewSource.GetDefaultView(this.Towns);
            return view;
        }

        public Country SelectedCountry
        {
            get
            {
                return GetDefaultCountriesView().CurrentItem as Country;
            }
        }

        public Town SelectedTown
        {
            get
            {
                return GetDefaultTownsView().CurrentItem as Town;
            }
        }

        void viewCountries_CurrentChanged(object sender, EventArgs e)
        {
            OnPropertyChanged("SelectedCountry");
            this.SubscribeToChangedEvent();
            OnPropertyChanged("Towns");
            //OnPropertyChanged("SelectedTown");
        }

        void viewTowns_CurrentChanged(object sender, EventArgs e)
        {
            OnPropertyChanged("Towns");
            OnPropertyChanged("SelectedTown");
        }

        public ICommand AddCountry
        {
            get
            {
                if (this.addCountry == null)
                {
                    this.addCountry = new ParameterRelayCommand((object obj) =>
                    {
                        try
                        {
                            Country newCountry = new Country();
                            newCountry.Name = (string)obj;
                            this.model.AddCountry(newCountry);
                            this.operationResult = "Successfully Added Country";
                        }
                        catch (Exception e)
                        {
                            this.operationResult = "Operation Unsuccessfull";
                        }
                        this.model.ReloadCountries();
                        this.OnPropertyChanged("Countries");

                        var view = GetDefaultCountriesView();
                        view.MoveCurrentToPrevious();
                        view.MoveCurrentToNext();
                        this.OnPropertyChanged("OperationResult");
                    });
                }
                return this.addCountry;
            }
        }

        public ICommand DeleteCountry
        {
            get
            {
                if (this.deleteCountry == null)
                {
                    this.deleteCountry = new ParameterRelayCommand((object obj) =>
                    {
                        try
                        {
                            this.model.DeleteCountry(obj as Country);
                            this.operationResult = "Successfully Deleted Country";
                        }
                        catch (Exception e)
                        {
                            this.operationResult = "Operation Unsuccessfull: " + e.Message;
                        }
                        this.model.ReloadCountries();
                        this.OnPropertyChanged("Countries");

                        var view = GetDefaultCountriesView();
                        view.MoveCurrentToPrevious();
                        view.MoveCurrentToNext();
                        this.OnPropertyChanged("OperationResult");
                    });
                }
                return this.deleteCountry;
            }
        }

        public ICommand SaveCountry
        {
            get
            {
                if (this.saveCountry == null)
                {
                    this.saveCountry = new ParameterRelayCommand((object obj) =>
                    {
                        try
                        {
                            this.model.SaveCountry(obj as Country);
                            this.operationResult = "Successfully Saved Country";
                        }
                        catch (Exception e)
                        {
                            this.operationResult = "Operation Unsuccessfull";
                        }
                        this.model.ReloadCountries();
                        this.OnPropertyChanged("Countries");

                        var view = GetDefaultCountriesView();
                        view.MoveCurrentToPrevious();
                        view.MoveCurrentToNext();
                        this.OnPropertyChanged("OperationResult");
                    });
                }
                return this.saveCountry;
            }
        }

        public ICommand AddTown
        {
            get
            {
                if (this.addTown == null)
                {
                    this.addTown = new ParameterRelayCommand((object obj) =>
                    {
                        int countryID = this.SelectedCountry.CountryID;
                        try
                        {
                            Town newTown = new Town();
                            newTown.Name = (string)obj;
                            newTown.CountryID = countryID;
                            this.model.AddTown(newTown);
                            this.operationResult = "Successfully Added Town";
                        }
                        catch (Exception e)
                        {
                            this.operationResult = "Operation Unsuccessfull";
                        }
                        this.model.ReloadTowns(countryID);
                        this.OnPropertyChanged("Towns");

                        var view = this.GetDefaultTownsView();
                        view.MoveCurrentToPrevious();
                        view.MoveCurrentToNext();
                        this.OnPropertyChanged("OperationResult");
                    });
                }
                return this.addTown;
            }
        }

        public ICommand DeleteTown
        {
            get
            {
                if (this.deleteTown == null)
                {
                    this.deleteTown = new ParameterRelayCommand((object obj) =>
                    {
                        int countryID = this.SelectedCountry.CountryID;
                        try
                        {
                            this.model.DeleteTown(obj as Town);
                            this.operationResult = "Successfully Deleted Town";
                        }
                        catch (Exception e)
                        {
                            this.operationResult = "Operation Unsuccessfull";
                        }
                        this.model.ReloadTowns(countryID);
                        this.OnPropertyChanged("Towns");

                        var view = this.GetDefaultTownsView();
                        view.MoveCurrentToPrevious();
                        view.MoveCurrentToNext();
                        this.OnPropertyChanged("OperationResult");
                    });
                }
                return this.deleteTown;
            }
        }
    }
}
